<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0" />
<title>API</title>
<link rel="stylesheet" href="/m/gui/ui.css?t=b"/>
<style>
#title {font-weight:bold;}
body {width:100%; height:100%; overflow:hidden;}
</style>
</head>
<body>

<div id="toolbar" style="margin-right:60px;">
	<form id="goForm" action="#">
		<input type="submit" value="GO" style="position:absolute; right:0px; padding:3px; margin:0px; width:50px;"/>
		<input type="text" id="url" value="" placeholder="api link" style="width:100%;"/>
	</form>
</div>

<iframe id="screen" src="http://local.me.ola.vn:8080/blank.html" style="width:100%; height:97%; border:none; margin:0; padding:0; background-color:#ffffff;"/></iframe>

<script type="text/javascript" src="/m/jquery-1.9.min.js?t=a"></script>
<script type="text/javascript" src="/m/js/app.js?t=a"></script>
<script type="text/javascript" src="/m/js/pages.js?t=a"></script>
<script>
$(document).ready(function() {
	$('#screen').height($(window).height() - $('#toolbar').height() - 5);
	$('#goForm').submit(function() {
		var url = $('#url').val();
		if (url) {
			url = mk(url);
			$('#screen').attr('src', url);
			$('#url').val('');
		} else {
			$('#url').val(document.getElementById('screen').contentWindow.location.href);
		}
		return false;
	});
});

var pl = /\+/g, search = /([^&=]+)=?([^&]*)/g, decode = function(s) {
	return decodeURIComponent(s.replace(pl, " "));
}, sortAz = function(a, b) {
	return ((a < b) ? -1 : ((a > b) ? 1 : 0))
};

function mk(url) {
	var owsc = $.cookie('_ola_working_code');
	var cid = '77B9A513';
	
	var a = url;
	if (a.indexOf('http://') > -1) {
		a = a.substring(a.indexOf('/', 10));
	}
	if (a.indexOf('/') == 0) {
		a = a.substring(1);
	}
	
	// a = 'abc.ocm?'
	var arr = [];
	arr.push('owsc=' + owsc);
	arr.push('cid=' + cid);

	var idx = a.indexOf('?');
	if (idx == a.length - 1) {
		a = a.substring(0, a.length - 1);
		idx = -1;
	}

	if (idx > -1) {
		/**
		 * Có tham số
		 */
		arr.push(a.substring(0, idx));

		var param = a.substring(idx + 1);
		while (match = search.exec(param)) {
			arr.push(decode(match[1]) + '=' + decode(match[2]));
		}
	} else {
		arr.push(a);
	}

	arr.sort(sortAz);
	var sig = $.md5(arr.join(' '));

	arr = [ url, (idx > -1 ? "&" : "?"), 'owsc=', owsc, '&cid=', cid, '&sig=', sig ];

	return arr.join('');
}

(function($) {
    $.cookie = function(key, value, options) {

        // key and at least value given, set cookie...
        if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
            options = $.extend({}, options);

            if (value === null || value === undefined) {
                options.expires = -1;
            }

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new Date();
                t.setDate(t.getDate() + days);
            }

            value = String(value);

            return (document.cookie = [
                encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                options.path    ? '; path=' + options.path : '',
                options.domain  ? '; domain=' + options.domain : '',
                options.secure  ? '; secure' : ''
            ].join(''));
        }

        // key and possibly options given, get cookie...
        options = value || {};
        var decode = options.raw ? function(s) { return s; } : decodeURIComponent;

        var pairs = document.cookie.split('; ');
        for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
            if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
        }
        return null;
    };
})(jQuery);
</script>
</body>
</html>
